let util = require('../../utils/until.js')

let api = require('../../config/api.js')

Page({
    data:{
        categoryList:[],
        currentCategory:{},
        currentsubCategoryList:{},
        goodsCount:0,
    },
    onLoad(options){
        this.getCategory()
    },
    getCategory:function(){
        //CategoryList
        let that = this
        wx.showLoading({
            title:'加载中...',
        })
        util.request(api.CategoryList).then(function(res){
            that.setData({
                categoryList:res.data.categoryList,
                currentCategory:res.data.currentCategory,
                currentsubCategoryList:res.data.currentSubCategory
            })
            wx.hideLoading();
        })
        util.request(api.GoodsCount).then(function(res){
            that.setData({
                goodsCount:res.data.goodsCount
            })
        })
    },
    //点击一级分类
    switchCate:function(event){
        let that = this
        let currentTarget = event.currentTarget;
        if(this.data.currentCategory.id == event.currentTarget.dataset.id){
            return false;
        }
        this.getCurrentCategory(event.currentTarget.dataset.id)
    },
    //获取二级分类
    getCurrentCategory:function(id){
        let that = this;
        util.request(api.CategoryCurrent,{
            id:id
        })
        .then(function(res){
            that.setData({
                currentCategory:res.data.currentCategory,
                currentsubCategoryList:res.data.currentSubCategory
            })
        })
    },
    //完成下拉刷新
    onPullDownRefresh(){
        wx.showNavigationBarLoading()
        this.getCategory()
        wx.hideNavigationBarLoading()
        wx.stopPullDownRefresh()
    }
})